/*
 * @Author: fisher
 * @Date: 2024-02-18 14:34:03
 * @LastEditors: fisher luthor-li@qq.com
 * @LastEditTime: 2024-03-12 17:18:04
 * @FilePath: \allfront\src\utils\index.js
 * @Description:
 */
/**
 * @description: 生成id
 * @param {*} len
 * @param {*} radix
 * @return {*}
 */
export const GenerateId = (len, radix) => {
  var chars =
    '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  var uuid = [],
    i;
  radix = radix || chars.length;
  if (len) {
    // Compact form
    for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];
  } else {
    // rfc4122, version 4 form
    var r;

    // rfc4122 requires these characters
    uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
    uuid[14] = '4';

    // Fill in random data.  At i==19 set the high bits of clock sequence as
    // per rfc4122, sec. 4.1.5
    for (i = 0; i < 36; i++) {
      if (!uuid[i]) {
        r = 0 | (Math.random() * 16);
        uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];
      }
    }
  }
  return uuid.join('').toLowerCase();
};

export const formatSeconds = seconds => {
  const days = Math.floor(seconds / 86400);
  const hours = Math.floor((seconds % 86400) / 3600);
  const minutes = Math.floor((seconds % 3600) / 60);
  const secs = seconds % 60;
  let timeString = '';

  if (days > 0) timeString += `${days}天`;
  if (hours > 0) timeString += `${hours}小时`;
  if (minutes > 0) timeString += `${minutes}分钟`;
  if (secs > 0) timeString += `${secs}秒`;

  return timeString || '0秒';
};

export function copyText(textToCopy) {
  var input = document.createElement('input');

  //将input的值设置为需要复制的内容

  input.value = textToCopy;

  //添加input标签

  document.body.appendChild(input);

  //选中input标签

  input.select();

  //执行复制

  document.execCommand('copy');

  //成功提示信息

  ElMessage({
    message: '复制成功！',
    type: 'success',
  });

  //移除input标签

  document.body.removeChild(input);
}
